﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIS.UI;
using System.Data;

public partial class Master_CurrencyRate : System.Web.UI.Page
{
    public Common _common = new Common();    

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            _common.LoadCurrencyDDL(ddlFromCurrency, string.Empty);
            _common.LoadCurrencyDDL(ddlToCurrency, string.Empty);
             
        }
    }

    protected void grvCurrencyRate_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearCurrencyRate();
        btnSubmit.Text = "Update";

        if (grvCurrencyRate.SelectedIndex >= 0)
        {
            DataKey _selectedDataKey = grvCurrencyRate.SelectedDataKey;
            Int16 _CurrencyRateKey = Convert.ToInt16(_selectedDataKey[0]);


            if (_CurrencyRateKey > 0)
            {
                GridViewRow _selectedRow = grvCurrencyRate.SelectedRow;

                LinkButton lnkEdit = (LinkButton)_selectedRow.FindControl("lnkbtnEdit");
                lnkEdit.Enabled = false;

                //LinkButton lnkDelete = (LinkButton)_selectedRow.FindControl("lnkbtnDelete");
                //lnkDelete.Enabled = false;

                CurrencyRateTable _CurrencyRate = CurrencyRateTableManager.GetItem(_CurrencyRateKey);
                if (_CurrencyRate != null)
                {
                    lblCurrencyRateKey.Text = _CurrencyRate.CurrencyRateKey.ToString();
                    ddlFromCurrency.SelectedValue = _CurrencyRate.FromCurrency;
                    ddlToCurrency.SelectedValue = _CurrencyRate.ToCurrency;
                    txtCurrencyRate.Text = _CurrencyRate.Rate.ToString("0.00");

                }

            }
        }
    }

    private void ClearCurrencyRate()
    {
        ddlFromCurrency.SelectedIndex = -1;
        ddlToCurrency.SelectedIndex = -1;
        txtCurrencyRate.Text = string.Empty;
    }

    protected void grvCurrencyRate_OnRowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
            Int16 _CurrencyRateKey = Convert.ToInt16(grvCurrencyRate.DataKeys[row.RowIndex].Value);
            bool lResult = CurrencyRateTableDB.Delete(_CurrencyRateKey);
            if (lResult)
            {
                lblMessage.Text = "Deleted Successfully";
            }
            else
            {
                lblMessage.Text = "Delet not Success";
            }
        }
        
         
    }    

    private void LoadInfo(short _CurrencyRateKey)
    {
        throw new NotImplementedException();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Master/CurrencyRate.aspx");
    }
    
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (Page.IsValid)
        {
            try
            {
                CurrencyRateTable _CurrencyRate = new CurrencyRateTable();
                _CurrencyRate.CurrencyRateKey = Convert.ToInt16(lblCurrencyRateKey.Text);
                _CurrencyRate.FromCurrency = ddlFromCurrency.SelectedValue;
                _CurrencyRate.ToCurrency = ddlToCurrency.SelectedValue;
                _CurrencyRate.Rate = Convert.ToDecimal(txtCurrencyRate.Text);
                _CurrencyRate.Status = true;
                _CurrencyRate.ModifiedDateTime = DateTime.Today;
                _CurrencyRate.CreatedDateTime = DateTime.Today;
                Int16 _CurrencyRaeKey = CurrencyRateTableManager.Save(_CurrencyRate);
                if (_CurrencyRaeKey > 0)
                {
                    ClearCurrencyRate();
                    grvCurrencyRate.DataBind();
                    lblMessage.Text = "Saved sussccessfully!";
                    lblMessage.ForeColor = System.Drawing.Color.Green;
                }
                else
                {
                    lblMessage.Text = "Saving failed!";
                    lblMessage.ForeColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception Ex)
            {
                lblMessage.Text = "Saving failed!<br/>" + Ex.Message;
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
        }
    }

    protected void grvCurrencyRate_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
         
        Int16 _CurrencyRateKey = Convert.ToInt16(grvCurrencyRate.DataKeys[e.RowIndex].Value);
        bool lResult = CurrencyRateTableDB.Delete(_CurrencyRateKey);
        if (lResult)
        {
            lblMessage.Text = "Deleted Successfully";
        }
        else
        {
            lblMessage.Text = "Delet not Success";
        }
    }

    protected void ToCurrencyServerValidate(object source, ServerValidateEventArgs args)
    {

        bool _retValue = true;
        string ToCurrency = ddlToCurrency.SelectedValue;
        if (ToCurrency.Equals("0"))
        {
            _retValue = false;
        }

        args.IsValid = _retValue;
    }

    protected void FromCurrencyServerValidate(object source, ServerValidateEventArgs args)
    {

        bool _retValue = true;
        string FromCurrency = ddlFromCurrency.SelectedValue;
        if (FromCurrency.Equals("0"))
        {
            _retValue = false;
        }

        args.IsValid = _retValue;
    }

    
}